var md5 = require('../../utils/util.js');
var Api = require("../../api/method.js");
//登录页
Page({
  data: {
    user: "",
    password: ""
  },
  //用户名
  getUser: function (e) {
    this.setData({
      user: e.detail.value
    })
  },
  //密码
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  //登录
  submit: function (e) {
    let that = this;
    if (this.data.user == "") {
      wx.showToast({
        icon: "none",
        duration: 1500,
        title: "用户名不能为空"
      })
      return false
    } else if (this.data.password == "") {
      wx.showToast({
        icon: "none",
        duration: 1500,
        title: "密码不能为空"
      })
      return false
    }else if(this.data.password.length < 8){
      wx.showToast({
        icon: "none",
        duration: 1500,
        title: "密码不能小于8位"
      })
      return false
    }else{
      let list = {
        accountNumber:that.data.user,
        password: md5.md5(that.data.password).toLowerCase()
      };
      console.log(list);
      //登录
      Api.Account(list, function (err, res) {
        console.log(res.data);
        if(res.data.code == 1){
          var token = res.data.data.token;
          var user = res.data.data.user;
          wx.setStorageSync("token", token);
          wx.setStorageSync("user", user);
          wx.showToast({
            icon: "none",
            duration: 1500,
            title: "登录成功"
          })
          if (res.data.data.user.roleCode=="branch"){
            wx.redirectTo({
              url: "/pages/branch/homepage/homepage",
            })
          }else if(res.data.data.user.roleCode =="center"){
            wx.redirectTo({
              url: "/pages/Generalhospital/homepage/homepage",
            })
          }
        }else{
          wx.showToast({
            icon: "none",
            duration: 1500,
            title: `${res.data.message}`
          })
        }
      }); 
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})